    <?php
    
    require_once($_SERVER['DOCUMENT_ROOT'] . '/alpha/smsclient.class.php');
    require_once($_SERVER['DOCUMENT_ROOT'] . '/phpmailer/class.phpmailer.php');
    
    class AlertSentClass{
        //public function __construct() {}

        function send_alfa($to, $message){
                $res = array();
                    $alfasms = new SMSclient();
                    //$message = $alfasms->translit($message);
                    $alfasms->sendSMS('MH', $to, $alfasms->translit($message));
                    if($alfasms->hasErrors()){
                        $res['success'] = false;
                        $res['msg'] = var_dump($alfasms->getErrors());
                    }
                    else{
                        $res['success'] = true;
                        $res['msg'] = "Отправлено: \r\n".$to;
                    }
                    unset($alfasms);
                return $res;
            }
            
            //IN: $to - array()
            function send_kyivstar($to, $message){
                $res = array();
                $php_mailer = new PHPMailer();
                $php_mailer->IsSMTP(); // send via SMTP                
                $php_mailer->AddReplyTo($php_mailer->From,$php_mailer->FromName);
                $php_mailer->IsHTML(false); // send as HTML
                $php_mailer->Body = $this->rus2translit($message); //HTML Body
                foreach ($to as $value) {
                    $php_mailer->AddAddress("38".$value."@sms.kyivstar.net",$value); // Recipient's e-mail, name
                }
                unset($value);
                if(!$php_mailer->Send()){
                    $res['success'] = false;
                    $res['msg'] = "Mailer Error: " . $php_mailer->ErrorInfo."\r\n";
                }
                else{
                    $res['success'] = true;
                    $res['msg'] = "Message has been successfully sent for kyivstar by free sms!\r\n";
                }
                unset($php_mailer);
                return $res;
            }
            
            //IN: $to - array()
            function send_email($to, $message, $link){
                $res = array();
                $php_mailer = new PHPMailer();
                $php_mailer->IsSMTP(); // send via SMTP                
                $php_mailer->AddReplyTo($php_mailer->From,$php_mailer->FromName);
                $php_mailer->IsHTML(false); // send as HTML
                $php_mailer->Body = $message.' '.$link; //HTML Body
                foreach ($to as $value) {
                    $php_mailer->AddAddress($value,$value); // Recipient's e-mail, name
                }
                unset($value);
                //$php_mailer->AddAddress($to,$to); // Recipient's e-mail, name
                if(!$php_mailer->Send()){
                    $res['success'] = false;
                    $res['msg'] = "Mailer Error: " . $php_mailer->ErrorInfo."\r\n";
                }
                else{
                    $res['success'] = true;
                    $res['msg'] = "Message has been successfully sent by email!\r\n";
                }
                unset($php_mailer);
                return $res;
            }
            
            function rus2translit($string)
            {
                $converter = array(
                    'а' => 'a',   'б' => 'b',   'в' => 'v',
                    'г' => 'g',   'д' => 'd',   'е' => 'e',
                    'ё' => 'e',   'ж' => 'zh',  'з' => 'z',
                    'и' => 'i',   'й' => 'y',   'к' => 'k',
                    'л' => 'l',   'м' => 'm',   'н' => 'n',
                    'о' => 'o',   'п' => 'p',   'р' => 'r',
                    'с' => 's',   'т' => 't',   'у' => 'u',
                    'ф' => 'f',   'х' => 'h',   'ц' => 'c',
                    'ч' => 'ch',  'ш' => 'sh',  'щ' => 'sch',
                    'ь' => "'",  'ы' => 'y',   'ъ' => "'",
                    'э' => 'e',   'ю' => 'yu',  'я' => 'ya',

                    'А' => 'A',   'Б' => 'B',   'В' => 'V',
                    'Г' => 'G',   'Д' => 'D',   'Е' => 'E',
                    'Ё' => 'E',   'Ж' => 'Zh',  'З' => 'Z',
                    'И' => 'I',   'Й' => 'Y',   'К' => 'K',
                    'Л' => 'L',   'М' => 'M',   'Н' => 'N',
                    'О' => 'O',   'П' => 'P',   'Р' => 'R',
                    'С' => 'S',   'Т' => 'T',   'У' => 'U',
                    'Ф' => 'F',   'Х' => 'H',   'Ц' => 'C',
                    'Ч' => 'Ch',  'Ш' => 'Sh',  'Щ' => 'Sch',
                    'Ь' => "'",  'Ы' => 'Y',   'Ъ' => "'",
                    'Э' => 'E',   'Ю' => 'Yu',  'Я' => 'Ya',
                );
                return strtr($string, $converter);
            }
            
            function caseEvent($event){
                switch($event){
                    case 'accident':
                        $ret = 'ДТП';
                        break;
                    case 'malfunction':
                        $ret = 'Поломка';
                        break;
                    default:
                        $ret = '';
                    return $ret;
                }
            }

            //IN: 	$selectIdDtp to get info
            //OUT: 	array([inf]=> {lat,lng,datatime, type, probable, confirm, user},
            //                [recipients]=>{[0]=>{id,user,email,is_alert,phone1,is_alert1,phone2,is_alert2,phone3,is_alert3,phone4,is_alert4},
            //                               [1]=>{...}
            //                              },
            //                [count]
            //               )
            function getDBdata($selectIdDtp = null){
                $ret = array();
                
                    $config_sql = file_get_contents("../../config_sql.txt");
                    $arr_config_sql =  explode("!",$config_sql);

                    $hostname= "$arr_config_sql[1]";
                    $username = "$arr_config_sql[2]";
                    $password = "$arr_config_sql[3]";
                    $table_db = "$arr_config_sql[4]";

                    $db = mysql_connect($hostname, $username, $password) //соединение с базой данных
                                    or die('connect to database failed');
                    mysql_select_db($table_db) or die('db not found'); //соединение  с базой данных
                    mysql_query("SET NAMES 'utf8'");
                    
                    if(!is_null($selectIdDtp)){
                        $res = mysql_query("SELECT * FROM information WHERE id='".$selectIdDtp."'");
                        while($row = mysql_fetch_array($res, 1)){
                            $ret['inf']['lat']      = preg_replace("/\s/","",$row['lat']);
                            $ret['inf']['lng']      = preg_replace("/\s/","",$row['lng']);   
                            $ret['inf']['type']     = $row['type'];
                        }
                    }
                    $res = mysql_query("SELECT * FROM contact ORDER BY 'id'"); // выборка емайлов
                    $count=0;
                    
                    while($row = mysql_fetch_array($res, 1)){
                        $ret['recipients'][$count] = $row;                 
                        $count++;
                    }
                    
                    mysql_free_result($res);
                    mysql_close($db);
                    $ret['count'] = $count;
                
                return $ret;
            }
    }

?>